package com.violet.detective.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.violet.detective.entity.Equipment;
import com.violet.detective.search.EquipmentSearchParameter;
import com.violet.detective.service.EquipmentService;
import com.violet.detective.util.ERCode;
import com.violet.detective.util.R;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@RestController
@RequestMapping("/equipment")
public class EquipmentController {
    @Resource
    private EquipmentService equipmentService;

    @PostMapping("/add")
//    public R addEquipment(@RequestBody Equipment equipment, @RequestBody MultipartFile file) throws IOException {
    public R addEquipment(@RequestBody MultipartFile file,
                          @RequestParam String name,
                          @RequestParam String unit,
                          @RequestParam String major) throws IOException {
        Equipment equipment = new Equipment();
        equipment.setName(name);
        equipment.setUnit(unit);
        equipment.setMajor(major);
        boolean result = equipmentService.addEquipment(equipment, file);
        return result ? R.ok() : R.error();
    }

    @PostMapping("/update")
    public R updateEquipment(@RequestBody Equipment equipment) {
        boolean result = equipmentService.editEquipment(equipment);
        return result ? R.ok() : R.error();
    }

    @GetMapping("/getImage")
    public void showImage(@RequestParam("imageName") String imageName, HttpServletResponse response) throws IOException {
        equipmentService.showImage(imageName, response);
    }

    @DeleteMapping("/deleteEquipment/{id}")
    public R deleteEquipment(@PathVariable Integer id) {
        try {
            boolean isSuccess = equipmentService.deleteEquipment(id);
            return isSuccess ? R.ok() : R.error();
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    @GetMapping("/getEquipmentById/{id}")
    public R getEquipmentById(@PathVariable Integer id) {
        return R.ok(equipmentService.getEquipmentById(id));
    }

    @GetMapping("/searchById")
    public R searchEquipment(@RequestParam int equipmentId) {
        Equipment result = equipmentService.searchEquipments(equipmentId);
        return R.ok(result);
    }

    @PostMapping("/search")
    public R searchEquipments(@RequestBody EquipmentSearchParameter searchParameter) {
        List<Equipment> result = equipmentService.searchEquipments(searchParameter);
        return R.ok(result);
    }

    @PostMapping("/page-search")
    public R searchEquipments(
            @RequestBody EquipmentSearchParameter searchParameter,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "5") int size
    ) {
        Page<Equipment> resultPage = equipmentService.searchEquipments(searchParameter, page, size);
        return R.ok(resultPage);
    }

    @GetMapping("/equipments")
    public R findAllEquipments() {
        List<Equipment> result = equipmentService.findAllEquipments();
        return R.ok(result);
    }

    @GetMapping("/page-equipments")
    public R findAllRepairs(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "5") int size
    ) {
        Page<Equipment> resultPage = equipmentService.findAllEquipments(page, size);
        return R.ok(resultPage);
    }

}
